package models

import (
	// "errors"
	"fmt"
	// "reflect"
	// "strings"
	// "github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"time"
)

type Receiv struct {
	Id       string    `orm:"column(id);pk"`
	OrderId  string    `orm:"column(order_id)"`
	Seq      int       `orm:"column(seq)"`
	Money    float64   `orm:"column(money)"`
	Date     time.Time `orm:"column(date);type(date)"`
	Remark   string    `orm:"column(remark);null"`
	Status   int       `orm:"column(status)"`
	Isdelete int       `orm:"column(isdelete)"`
	Ctime    time.Time `orm:"column(ctime);type(timestamp without time zone)"`
	Creater  string    `orm:"column(creater);null"`
	Utime    time.Time `orm:"column(utime);type(timestamp without time zone)"`
	Updater  string    `orm:"column(updater);null"`
}

func (t *Receiv) TableName() string {
	return "yjk_receiv"
}

func init() {
	orm.RegisterModel(new(Receiv))
}

// AddReceiv insert a new Receiv into database and returns
// last inserted Id on success.
func AddReceiv(m *Receiv) (id int64, err error) {
	o := orm.NewOrm()
	id, err = o.Insert(m)
	return
}

// GetReceivById retrieves Receiv by Id. Returns error if
// Id doesn't exist
func GetReceivById(id string) (v *Receiv, err error) {
	o := orm.NewOrm()
	v = &Receiv{Id: id}
	if err = o.Read(v); err == nil {
		return v, nil
	}
	return nil, err
}

// GetAllReceiv retrieves all Receiv matches certain condition. Returns empty list if
// no records exist
func GetAllReceiv() (ml []orm.Params, err error) {
	o := orm.NewOrm()
	// qs := o.QueryTable(new(Receiv))
	sql := "select a.*,b.name,b.cust_name from yjk_receiv a INNER JOIN yjk_order b " +
		" on a.order_id=b.id where b.status=2 and b.isdelete=0 "
	_, err = o.Raw(sql).Values(&ml)
	return
}

// UpdateReceiv updates Receiv by Id and returns error if
// the record to be updated doesn't exist
func UpdateReceivById(m *Receiv) (err error) {
	o := orm.NewOrm()
	v := Receiv{Id: m.Id}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		var num int64
		if num, err = o.Update(m); err == nil {
			fmt.Println("Number of records updated in database:", num)
		}
	}
	return
}

// DeleteReceiv deletes Receiv by Id and returns error if
// the record to be deleted doesn't exist
func DeleteReceiv(id string) (err error) {
	o := orm.NewOrm()
	v := Receiv{Id: id}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		var num int64
		if num, err = o.Delete(&Receiv{Id: id}); err == nil {
			fmt.Println("Number of records deleted in database:", num)
		}
	}
	return
}

// GetReceivById retrieves Receiv by Id. Returns error if
// Id doesn't exist
func GetReceivByOrderId(id string) (v []Receiv, err error) {
	o := orm.NewOrm()
	qs := o.QueryTable(new(Receiv)).Filter("OrderId", id).Limit(-1).OrderBy("Seq")
	_, err = qs.All(&v)
	return
}

// DeleteReceivByOrder 删除 Receiv by orderId and returns error
func DeleteReceivByOrder(id string) (err error) {
	o := orm.NewOrm()
	qs := o.QueryTable(new(Receiv))
	_, err = qs.Filter("OrderId", id).Delete()
	return
}
